<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2022/7/28
 * Time: 10:52
 */

namespace app\project\controller;


use app\common\controller\Common;
use app\project\service\PublicService;
use think\Controller;

class Progress extends Controller
{

    //列表
    public function index()
    {
        $pro_id = $_GET['pro_id'];
        $group = $_GET['group'];
        $assign['pro_id'] = $pro_id;
        $assign['group'] = $group;
        $this->assign($assign);
        if (!empty($_GET['type'])) {
            try {
                $common = new Common();
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $where['pid'] = $_GET['pro_id'];
                $where['type'] = $_GET['group'];
                $where['addtime'] = ['gt',0];

                $proDetail = PublicService::detailService("project_pro",['id'=>$pro_id],1);

                $list = $common->public_select("project_progress", $where, null, $start, $limit)->order("id desc")->select();
                foreach ($list as $k => $v) {
                    $list[$k]['addtime'] = date("Y-m-d H:i:s", $v['addtime']);

                    if($proDetail['library']==6){
                        $list[$k]['progress'] = $v['work'];
                    }
                    
                    $list[$k]['work'] = $v['work']??$v['progress'];
//                    $v['work']

                }
                $count = $common->public_select("project_progress", $where)->count("*");
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            } catch (Exception $e) {
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        } else {
            return $this->fetch("index");
        }
    }


    //详情页面
    public function detail()
    {
        $id = $_GET['id'];
        $where['id'] = $id;
        $common = new Common();
        $detail = $common->public_select("project_progress", $where)->find();
        $detail['addtime'] = date("Y-m-d H:i:s", $detail['addtime']);
        $detail['images'] = empty($detail['images'])?[]:explode(";",$detail['images']);
        $assign['detail'] = $detail;

        //项目详细信息
        $proDetail = $common->public_select("project_pro",['id'=>$detail['pid']])->find();

        if ($detail['type'] == 1) {

            if($proDetail['library'] == 6){
                if($proDetail['enterprise']==352){
                    $field = [
                        "work" => ['name' => "进展情况", "type" => "text"],
                        "question" => ['name' => "存在的问题", "type" => "text"],
                        "images" => ['name' => "图片", "type" => "image"],
                        "addtime" => ['name' => "提交时间", "type" => "text"],
                    ];
                }else{
                    $field = [
                        "work" => ['name' => "年度累计应税收入", "type" => "text"],
                        "question" => ['name' => "存在的问题", "type" => "text"],
                        "images" => ['name' => "图片", "type" => "image"],
                        "addtime" => ['name' => "提交时间", "type" => "text"],
                    ];
                }
            }else{
                $field = [
                    "work" => ['name' => "总体进展情况", "type" => "text"],
                    "progress" => ['name' => "目前进度情况", "type" => "text"],
                    "last_work" => ['name' => "下周工作安排", "type" => "text"],
                    "question" => ['name' => "存在的问题", "type" => "text"],
                    "images" => ['name' => "图片", "type" => "image"],
                    "addtime" => ['name' => "提交时间", "type" => "text"],
                ];
            }
        }else if($detail['type'] == 3){
            $field = [
                "progress" => ['name' => "逾期原因", "type" => "text"],
                "images" => ['name' => "佐证", "type" => "image"],
                "addtime" => ['name' => "提交时间", "type" => "text"],
            ];
        }else if($detail['type'] == 4){
            $field = [
                "progress" => ['name' => "情况说明", "type" => "text"],
                "images" => ['name' => "佐证", "type" => "image"],
                "addtime" => ['name' => "提交时间", "type" => "text"],
            ];
        } else {
            $field = [
                "work" => ['name' => "总体进展情况", "type" => "text"],
                "progress" => ['name' => "本月进展", "type" => "text"],
                "last_work" => ['name' => "下月计划", "type" => "text"],
                "question" => ['name' => "存在的问题", "type" => "text"],
                "addtime" => ['name' => "提交时间", "type" => "text"],
            ];
        }
        $assign['field'] = $field;
        $this->assign($assign);
        return $this->fetch();
    }

    //删除操作
    public function del()
    {
        try {
            $common = new Common();
            $id = $_POST['id'];
            $common->public_select("project_progress", ['id' => ['in', $id]])->delete();
            $black['code'] = 0;
            $black['msg'] = "删除成功";
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}